package com.koala.app;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

/* Class needed so we can extend from ListFragment */
public class DogsListFragment extends ListFragment {
	private List<String> dogs;
	private DogAdapter dogListAdapter;
	 
	public DogsListFragment() {
		dogs = new ArrayList<String>();
	}
	  
	  
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	   
		dogListAdapter = new DogAdapter(getActivity(), dogs);
		setListAdapter(dogListAdapter);
	}
	  
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.fragment_main, container, false);
		
		// Add dog list filter
		EditText dogFilterTxt = (EditText) rl.findViewById(R.id.dogFilterTxt);
		dogFilterTxt.addTextChangedListener(new TextWatcher() {
	    	  
	        @Override
	        public void onTextChanged(CharSequence s, int start, int before, int count) {
	        	//dogListAdapter.getFilter().filter(s.toString());
	        	dogListAdapter.filter(s.toString());
	        }
	         
	        @Override
	        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	             
	        }
	         
	        @Override
	        public void afterTextChanged(Editable s) {
	        	
	        }
	    });
		return rl;
	}
	  
	
	@Override
	public void onListItemClick(ListView list, View v, int position, long id) {
		
		Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
	}


	/** Set the dogs list 
	 * @param dogs the dogs to set
	 */
	public void setDogs(List<String> dogs) {
		this.dogs = dogs;
	}

	
	
}
